package com.star.spring.websocket.controller;

import com.star.spring.websocket.handler.WebSocketHandler;
import com.star.spring.websocket.client.WebSocketClientHandler;
import com.star.spring.websocket.model.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/api/websocket")
public class WebSocketController {

    @Autowired
    private WebSocketHandler webSocketHandler;

    @Autowired
    private WebSocketClientHandler webSocketClientHandler;

    /**
     * 获取连接状态
     */
    @GetMapping("/status")
    public Map<String, Object> getStatus() {
        Map<String, Object> status = new HashMap<>();
        status.put("serverConnections", webSocketHandler.getConnectionCount());
        status.put("clientStatus", webSocketClientHandler.getConnectionStatus());
        status.put("clientConnected", webSocketClientHandler.isConnected());
        return status;
    }

    /**
     * 获取所有会话ID
     */
    @GetMapping("/sessions")
    public Set<String> getSessions() {
        return webSocketHandler.getAllSessionIds();
    }

    /**
     * 发送消息给指定会话
     */
    @PostMapping("/send/{sessionId}")
    public Map<String, Object> sendMessage(@PathVariable String sessionId, @RequestBody Message message) {
        Map<String, Object> result = new HashMap<>();
        try {
            webSocketHandler.sendMessage(sessionId, message);
            result.put("success", true);
            result.put("message", "消息发送成功");
        } catch (Exception e) {
            log.error("发送消息失败", e);
            result.put("success", false);
            result.put("message", "消息发送失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 广播消息给所有连接
     */
    @PostMapping("/broadcast")
    public Map<String, Object> broadcastMessage(@RequestBody Message message) {
        Map<String, Object> result = new HashMap<>();
        try {
            webSocketHandler.broadcastMessage(message);
            result.put("success", true);
            result.put("message", "广播消息发送成功");
        } catch (Exception e) {
            log.error("广播消息失败", e);
            result.put("success", false);
            result.put("message", "广播消息失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 客户端连接控制
     */
    @PostMapping("/client/connect")
    public Map<String, Object> connectClient() {
        Map<String, Object> result = new HashMap<>();
        try {
            webSocketClientHandler.connect();
            result.put("success", true);
            result.put("message", "客户端连接请求已发送");
        } catch (Exception e) {
            log.error("客户端连接失败", e);
            result.put("success", false);
            result.put("message", "客户端连接失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 客户端断开连接
     */
    @PostMapping("/client/disconnect")
    public Map<String, Object> disconnectClient() {
        Map<String, Object> result = new HashMap<>();
        try {
            webSocketClientHandler.disconnect();
            result.put("success", true);
            result.put("message", "客户端断开连接成功");
        } catch (Exception e) {
            log.error("客户端断开连接失败", e);
            result.put("success", false);
            result.put("message", "客户端断开连接失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 客户端发送消息
     */
    @PostMapping("/client/send")
    public Map<String, Object> sendClientMessage(@RequestBody Message message) {
        Map<String, Object> result = new HashMap<>();
        try {
            webSocketClientHandler.sendMessage(message);
            result.put("success", true);
            result.put("message", "客户端消息发送成功");
        } catch (Exception e) {
            log.error("客户端发送消息失败", e);
            result.put("success", false);
            result.put("message", "客户端发送消息失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 测试消息发送
     */
    @PostMapping("/test/send")
    public Map<String, Object> testSendMessage(@RequestParam String sessionId, @RequestParam String content) {
        Map<String, Object> result = new HashMap<>();
        try {
            Message testMessage = new Message();
            testMessage.setType("TEST");
            testMessage.setContent(content);
            testMessage.setTimestamp(System.currentTimeMillis());
            
            webSocketHandler.sendMessage(sessionId, testMessage);
            result.put("success", true);
            result.put("message", "测试消息发送成功");
        } catch (Exception e) {
            log.error("测试消息发送失败", e);
            result.put("success", false);
            result.put("message", "测试消息发送失败: " + e.getMessage());
        }
        return result;
    }
} 